<?php

namespace app\api\controller;

use app\common\Common;
use app\api\service\RecordService;
use think\facade\Request;

class Record extends Base
{
    private $service;

    public function __construct(RecordService $service)
    {
        $this->service = $service;
    }

    /**
     * 添加观看记录
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param int video_detail_id
     * @return fixed
     */
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Record::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editRecord($params['video_detail_id'], $this->getUserID());
        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 获取追剧数据
     * 
     * @dateTime 2022/09/03
     * @author zhixiang0215@126.com
     * @return array
     */
    public function getBingeWatchList()
    {
        $params = Request::param();
        $data = $this->service->getBingeWatchList($params, $this->getUserID());
        return Common::success($data);
    }

    /**
     * 获取历史记录
     * 
     * @dateTime 2022/09/03
     * @author zhixiang0215@126.com
     * @return array
     */
    public function getHistoryList()
    {
        $params = Request::param();
        $data = $this->service->getHistoryList($params, $this->getUserID());
        return Common::success($data);
    }

    /**
     * 添加追剧记录
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param int video_detail_id
     * @param int video_id
     * @param int sort
     * @return fixed
     */
    public function addPlay()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Record::class)->scene('add_play')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editBingeWatch($params, $this->getUserID(), 1);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 取消追剧
     */
    public function cancelPlay()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Record::class)->scene('cancel_play')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editBingeWatch($params, $this->getUserID(), 2);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 获取充值记录
     */
    public function getPayRecord()
    {
        $params = Request::param();

        $data = $this->service->getUserRechangeRecord($params, $this->getUserID());

        return Common::success($data);
    }

    /**
     * 获取用户消费记录
     */
    public function getWatchPayRecord()
    {
        $params = Request::param();
        $data = $this->service->getWatchPayRecord($params, $this->getUserID());

        return Common::success($data);
    }
    /**
     * 支付查询
     */
    public function getPayOrder(){
        $params = Request::param();
        $data = $this->service->PayOrderStatus($params, $this->getUserID());
        return Common::success($data);
    }
}